package com.zong;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.zong.zdb.service.JdbcCodeService;

/**
 * @desc springboot总配置类和启动入口类
 * @author zong
 * @date 2017年3月21日
 */
@SpringBootApplication
@ServletComponentScan
@MapperScan(basePackages = "com.zong.**.dao")
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer {
	private static Logger logger = LoggerFactory.getLogger(Application.class);

	@Autowired
	private Environment env;

	@Bean
	public JdbcCodeService getJdbcCodeService() {
		JdbcCodeService jdbcCodeService = JdbcCodeService.getInstance();
		jdbcCodeService.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));
		jdbcCodeService.setUrl(env.getProperty("spring.datasource.url"));
		jdbcCodeService.setUsername(env.getProperty("spring.datasource.username"));
		jdbcCodeService.setPassword(env.getProperty("spring.datasource.password"));
		return jdbcCodeService;
	}

	/**
	 * 打包war发布到tomcat运行需要继承 SpringBootServletInitializer并重写 configure 方法
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		logger.info("zboot项目启动");
	}

}